package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService service;
    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        //1. 构建分页模型
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //2. 组装查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);
        //组装排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //分页查询
        service.page(setmealPage);


        //构建DTO分页模型, 将setmealPage数据进行拷贝并转换
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        //处理分类名称
        List<SetmealDto> dtos = setmealPage.getRecords().stream().map(setmeal -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,dto);

            Long categoryId = setmeal.getCategoryId();
            //根据分类id查询分类名称
            Category category = categoryService.getById(categoryId);
            if (category != null){
                dto.setCategoryName(category.getName());
            }
            return dto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(dtos);

        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐(单个/批量
     * @param ids
     * @return
     * tips: 用数组接,保证参数名和键名一致即可!
     *       用集合接,必须加@RequestParam注解!
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)  //清除setmealCache名称下所有缓存数据
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("进行删除套餐操作,入参ids:{}", ids);
        service.removeWithDish(ids);
        return R.success("删除成功!");
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        service.add(setmealDto);
        return R.success("新增套餐成功!");
    }

    /**
     * 根据条件: 查询套餐
     * @param setmeal(categoryId,status)
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#p0.categoryId + '_' + #p0.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = service.list(wrapper);
        return R.success(list);
    }
}
